#!/bin/bash

# Slackware build script for jamulus

# Written by B. Watson (urchlay@slackware.uk)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20230112 bkw: update for v3.9.1.
# 20211207 bkw:
# - update for v3.8.1.
# - add 'headless' option to SERVERONLY=yes config.
# - new-style icons.
# - man page.
# - dynamic slack-desc.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=jamulus
VERSION=${VERSION:-3.9.1}
BUILD=${BUILD:-1}

TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

# Check this before doing anything else.
if [ "${SERVERONLY:-no}" = "yes" ]; then
  EXTRACONF="CONFIG+=nosound CONFIG+=headless"
elif ! pkg-config --exists jack; then
  cat <<EOF

***********************************************************************
$0: jack not found.

If you want $PRGNAM to be able to make sound, you must install
jack before running this script.

If you want to build a dedicated server, you don't have to install
jack. Instead, export SERVERONLY=yes in the environment. See SERVER.txt
for details.
***********************************************************************

EOF
  sleep 5
  exit 1
fi

TARVER=r"${VERSION//./_}"

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$TARVER
tar xvf $CWD/$PRGNAM-$TARVER.tar.gz
cd $PRGNAM-$TARVER
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

qmake-qt5 \
  "CONFIG+=noupcasename" \
  $EXTRACONF \
  QMAKE_CFLAGS="$SLKCFLAGS" QMAKE_CXXFLAGS="$SLKCFLAGS" \
  PREFIX=/usr \
  Jamulus.pro
make clean
make
make install INSTALL_ROOT=$PKG

# binary already stripped.

# Man page taken from the sourceand modified slightly.
# Had to make the man page a "1x" instead of "1": It uses BSD macros
# (see groff_mdoc(7)). If I made it a "1" man page, it would say
# "BSD General Commands Manual" unconditionally (no way to force it
# to say "SlackBuilds.org" or even "General Commands Manual").
mkdir -p $PKG/usr/man/man1
gzip -9c < $CWD/$PRGNAM.1x > $PKG/usr/man/man1/$PRGNAM.1x.gz
rm -rf $PKG/usr/share/man # original page (in wrong place)

if [ "${SERVERONLY:-no}" != "yes" ]; then
  # The only png icon provided is 512x512, ludicrously large, make smaller ones.
  # Note to self: use rsvg-convert rather than ImageMagick's convert, because
  # IM behaves differently depending on whether inkscape is installed. It
  # wouldn't really hurt anything, I just want to be consistent.
  HICOLOR=$PKG/usr/share/icons/hicolor/
  ICNAME=io.jamulus.jamulus

  for px in 16 32 48 64 128; do
    size=${px}x${px}
    dir=$HICOLOR/$size/apps
    mkdir -p $dir
    CVT="rsvg-convert --width=$px --height=$px"
    $CVT src/res/$ICNAME.svg > $dir/$ICNAME.png
    $CVT src/res/${ICNAME}server.svg > $dir/${ICNAME}server.png
  done

  mkdir -p $PKG/usr/share/pixmaps
  ln -s ../icons/hicolor/48x48/apps/$ICNAME.png $PKG/usr/share/pixmaps/$PRGNAM.png
fi

# NEWS is a 0-byte placeholder in 3.4.3... and nonexistent in 3.9.1
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a COPYING ChangeLog CONTRIBUTING.md README.md SECURITY.md \
      $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

# Include our own (hopefully) helpful hints for servers.
cat $CWD/SERVER.txt > $PKG/usr/doc/$PRGNAM-$VERSION/SERVER.txt

if objdump -p $PKG/usr/bin/$PRGNAM | grep -q 'NEEDED.*libjack'; then
  DESC="full client and server"
else
  DESC="headless server only"
fi

mkdir -p $PKG/install
sed "s,@DESC@,$DESC," $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

# Only add capability stuff if not disabled:
if [ "${SERVERONLY:-no}" != "yes" ]; then
  if [ "${SETCAP:-yes}" = "yes" ]; then
    cat $CWD/setcap.sh >> $PKG/install/doinst.sh
    # Only allow execution by audio group
    chown root:audio $PKG/usr/bin/$PRGNAM
    chmod 0750 $PKG/usr/bin/$PRGNAM
  fi
fi

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
